﻿<!doctype html>
<html>
<head>
  <title>javaScript实现文字跑马灯</title>
  <style type="text/css">
    *{ margin:0; padding:0;}
    body{font:12px/1 '微软雅黑';}
    .wrapper{font-size: 0.85rem; 
			color: #333; 
			padding-top: 0.75rem; 
			margin: 0 0.75rem; 
			white-space: nowrap; 
			overflow: hidden;
			width: 300px;}
    .inner{ width:1000px;
			overflow:hidden;
			background-image: url("http://www.webdm.cn/images/20130228/LEDbj.gif");
			padding-top: 8px;
			padding-right: 5px;
			padding-left: 5px;
			height: 50px;}
    .inner p{ display:inline-block;color: #FF0000;font-size:35px}
  </style>
</head>
<body>
  <div id="wrapper" class="wrapper">
    <div class="inner">
      <p class="txt">欢迎学习JavaScript！</p>
    </div>
  </div>
<script>
  var wrapper = document.getElementById('wrapper');
  var inner = wrapper.getElementsByTagName('div')[0];
  var p = document.getElementsByTagName('p')[0];
  var p_w = p.offsetWidth;
  var wrapper_w = wrapper.offsetWidth;
  window.onload=function fun(){  
    if(wrapper_w > p_w){ return false;}
    inner.innerHTML+=inner.innerHTML;
    setTimeout("fun1()",2000);    
  }
  function fun1(){    
    if(p_w > wrapper.scrollLeft){
      wrapper.scrollLeft++;
      setTimeout("fun1()",30);
    }
    else{
      setTimeout("fun2()",2000);
    }
  }
  function fun2(){
    wrapper.scrollLeft=0;
    fun1();
  }
</script>
</body>
</html>
